<template>
  <div class="hm-login">

    <el-card class="card">
      <div class="logo"></div>
      <el-form ref="form" :model="form" :rules="rules" width="80px">
        <el-form-item prop="username">
          <el-input v-model="form.username" prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" prefix-icon="el-icon-lock" type="password"></el-input>
        </el-form-item>
      </el-form>
      <el-button type="primary" @click="submit">登录</el-button>
      <el-button type="info" @click="reset">重置</el-button>
    </el-card>
  </div>
</template>

<script>
import { getLogin } from '@/api/user'

export default {
  name: 'hm_login',
  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          {
            required: true,
            message: '请输入用户名'
          },
          {
            min: 5,
            max: 11,
            message: '长度在 5 到 11 个字符'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码'
          },
          {
            pattern: /^\w{5,11}$/,
            message: '请输入 5 到 10 位的密码',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  methods: {
    async submit () {
      try {
        await this.$refs.form.validate()
        const res = await getLogin(this.form)
        console.log(res)
        this.$store.commit('user/setToken', res.data.token)
        await this.$router.push('/')
      } catch (e) {
        console.log(e)
      }
    },
    reset () {
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.hm-login {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #2a4b6b;
  //position: relative;
  .card {
    padding-top: 85px;
    width: 450px;
    height: 300px;
    position: relative;
    overflow: visible;

    .logo {
      z-index: 1;

      position: absolute;
      top: -25%;
      left: 50%;
      transform: translateX(-50%);
      width: 130px;
      height: 130px;
      border: 5px solid #fff;
      background-color: #ccc;
      border-radius: 50%;
      background: url("https://ss3.baidu.com/-fo3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/9825bc315c6034a83a5cbca2cc13495409237652.jpg") no-repeat -23px -1px/cover;
      box-shadow: 0 0 20px #ccc;
    }

  }
}
</style>
